<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">
    <h1>待办事项</h1>
    <input type="text" v-model="thing">
    <button @click="add">添加</button>
    <div v-show="todoThings.length">
      <p>待办事项</p>
      <ul>
        <li v-for="(item, index) in todoThings">
          <span>{{item}}</span>
          <button @click="done(index)">完成</button>
        </li>
      </ul>
    </div>
    <div v-show="doneThings.length">
      <p>完成事项</p>
      <ul>
        <li v-for="(item, index) in doneThings">
          <span>{{item}}</span>
        </li>
      </ul>
    </div>

  </div>
  <script src="./vue.js"></script>
  <script>
    let vm = new Vue({
      el: "#app",
      data: {
        // 事项，输入框中的值
        thing: '',
        // 待办事项的数组
        todoThings: JSON.parse(localStorage.getItem('todoThings')) || [],
        // 完成事项的数组
        doneThings: JSON.parse(localStorage.getItem('doneThings')) || [],
      },
      methods: {
        add() {
          if(this.thing) {
            // 添加到待办事项
            this.todoThings.push(this.thing);
            localStorage.setItem('todoThings', JSON.stringify(this.todoThings));
            // 输入框置空
            this.thing = '';
          }
        },
        done(index) {
          // 先添加到完成事项中，避免index失效
          this.doneThings.push(this.todoThings[index]);
          localStorage.setItem('doneThings', JSON.stringify(this.doneThings));
          // 从待办事项中删除
          this.todoThings.splice(index, 1);
          localStorage.setItem('todoThings', JSON.stringify(this.todoThings));
        }
      },
    })
  </script>
</body>

</html>